/*
 * @lc app=leetcode.cn id=202 lang=javascript
 *
 * [202] 快乐数
 */

// @lc code=start
/**
 * @param {number} n
 * @return {boolean}
 */
var isHappy = function(n) {
  const getList = (num) =>
    num
      .toString()
      .split("")
      .map((item) => parseInt(item));
  const getSum = (list) => list.reduce((sum, cur) => sum + cur * cur, 0);

  let slow = n;
  let fast = getSum(getList(n));
  while(fast !== 1 && fast !== slow) {
    slow = getSum(getList(slow));
    fast = getSum(getList(getSum(getList(fast))));
  }
  return fast === 1;
};
// @lc code=end

